package com.feihao.zhou.demo03_TreeMap;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.BiConsumer;

public class TestTreeMapSavingStudent {
    public static void main(String[] args) {
        TreeMap<Student, String> treeMap = new TreeMap<>();

        //创建学生对象：
        Student s1 = new Student("小三", 18);
        Student s2 = new Student("大浪", 19);
        Student s3 = new Student("文化", 18);

        treeMap.put(s1, "湖南");
        treeMap.put(s2, "北京");
        treeMap.put(s3, "广东");

        System.out.println(treeMap);


        System.out.println("=================");

        //遍历：keyset:
        Set<Student> keySet = treeMap.keySet();
        for (Student key : keySet) {
            //键找值：
            String value = treeMap.get(key);
            System.out.println(key + "来自" + value);
        }

        System.out.println("-------======--------");

//        entrySet遍历：
        Set<Map.Entry<Student, String>> entries = treeMap.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            String value = entry.getValue();
            Student key = entry.getKey();
            System.out.println(key + "来自" + value);
        }

        System.out.println("================");

        //foreach:lambda表达式进行遍历：
        treeMap.forEach(new BiConsumer<Student, String>() {
            @Override
            public void accept(Student student, String s) {
                System.out.println(student+ "== " +s);
            }
        });

    }
}
